<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="newsDB.*,newsEntity.*,java.sql.*,java.io.*" %>
<%@ page import="org.apache.commons.fileupload.*,org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@include file="../checkisAdmin.jsp"%>
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>新闻发布系统管理后台</title>
<link href="css/admin.css" rel="stylesheet" type="text/css" />
<div id="header">
  <div id="welcome">欢迎使用新闻管理系统！</div>
  <div id="nav">
    <div id="logo"><img 
src="images/logo.jpg" alt="新闻中国" /></div>
    <div id="a_b01"><img src="images/a_b01.gif" alt="" /></div>
  </div>
</div>
<div id="admin_bar">
</div>
</head>
<body>

<div id="main">
  <div id="opt_list">
  <ul>
    <li><a href="util/news_control.jsp?opr=findTopics">添加新闻</a></li>
    <li><a href="util/news_control.jsp?opr=list">编辑新闻</a></li>
    <li><a href="topic_add.jsp">添加主题</a></li>
    <li><a href="TopicServlet.jsp">编辑主题</a></li>
  </ul>
</div>

  <div id="opt_area">    
    <ul class="classlist">
		<%
		request.setCharacterEncoding("utf-8"); 
    	String opr=request.getParameter("opr");
    	if(opr.equals("topicNew")){
    		Long ntid=Long.parseLong(request.getParameter("tid"));
    		session.setAttribute("ntid", ntid);
    		response.sendRedirect("showList.jsp");
    		return;
    	}else if(opr.equals("del")){
    		Long nid=Long.parseLong(request.getParameter("nid"));
    		NewsDao.nd.delNews(nid);
    		response.sendRedirect("admin.jsp");
    	}else if(opr.equals("addComment")){
    		String cauthor=request.getParameter("cauthor");
    		String cip=request.getParameter("cip");
    		String ccontent=request.getParameter("ccontent");
    		Long cnid=Long.parseLong(request.getParameter("nid"));
    		CommentsDao.cd.addComments(cauthor, cip, ccontent, cnid);
    		response.sendRedirect("../news.jsp?nid="+cnid);
    	}else if(opr.equals("findTopics")){
    		response.sendRedirect("../addnews.jsp");
    	}else if(opr.equals("list")){
    		response.sendRedirect("../admin.jsp");
    	}else if(opr.equals("addTopic")){
    		String tname=request.getParameter("tname");
    		int row=TopicDao.td.addTopic(tname);
    		if(row>0){
    		String ref = request.getHeader("REFERER");
    		%>
    		<h2>增加成功</h2>
    		<input type="button" onclick="javascript:window.location='<%=ref %>'" value="确认"></input>
    		<%
    		return;
    		}
    		%>
    		<h2>主题重复</h2>
    		<input type="button" onclick="javascript:history.back()" value="返回"></input>
    		<%
    	}else if(opr.equals("changeTopic")){
    		String tname=request.getParameter("tname");
    		Long tid=Long.parseLong(request.getParameter("tid"));
    		int row=TopicDao.td.changeTopic(tid, tname);
    		if(row>0){
    		String ref = request.getHeader("REFERER");
    		%>
    		<h2>修改成功</h2>
    		<input type="button" onclick="javascript:window.location='<%=ref %>'" value="确认"></input>
    		<%
    		return;
    		}
    		%>
    		<h2>主题重复</h2>
    		<input type="button" onclick="javascript:history.back()" value="返回"></input>
    		<%
    	}else if(opr.equals("addNews")){
    		boolean isMultipart=ServletFileUpload.isMultipartContent(request);
    		if(isMultipart){
    		DiskFileItemFactory factory=new DiskFileItemFactory();
    		ServletFileUpload upload=new ServletFileUpload(factory);
    		factory.setSizeThreshold(4096);
    		upload.setHeaderEncoding("utf-8");
    		String uploadPath=application.getRealPath("uploadImg");
    		String uploadTempPath=application.getRealPath("uploadTemp");
    		upload.setSizeMax(1024*500);
    		try{
    		List<FileItem> items=upload.parseRequest(request);
    		Long ntid=null;
    		String ntitle=null;
    		String nauthor=null;
    		String nsummary=null;
    		String ncontent=null;
    		String picName=null;
    		String newFileName=null;
    		for(FileItem f:items){
    			if(f.isFormField()){
    				if(f.getFieldName().equals("ntid")){
    					ntid=Long.parseLong(f.getString());
    				}else if(f.getFieldName().equals("ntitle")){
    					ntitle=f.getString("utf-8");
    				}else if(f.getFieldName().equals("nauthor")){
    					nauthor=f.getString("utf-8");
    				}else if(f.getFieldName().equals("nsummary")){
    					nsummary=f.getString("utf-8");
    				}else if(f.getFieldName().equals("ncontent")){
    					ncontent=f.getString("utf-8");
    				}
    			}else{
    				if(f.getName()==null||f.getName()==""){
    					continue;
    				}
    				List<String> typeList=Arrays.asList("gif","bmp","jpg","png","jpeg");
    				picName=f.getName();
    				int index=picName.lastIndexOf(".");
    				String typeName=picName.substring(index+1);	
    				if(typeList.contains(typeName)){
    					if(picName!=null&&!picName.equals("")){
    						newFileName=UUID.randomUUID().toString()+"."+typeName;
    						f.write(new File(uploadPath,newFileName));
    					}
    				}else{
    					%>
				<h2>文件类型错误</h2>
				<input type="button" onclick="javascript:window.location='addnews.jsp'" value="确定"></input>
						<%
    					return;
    				}
    			}
    			
    		}
    		int row=0;
    		if(newFileName==null||newFileName.equals("")){
    			row=NewsDao.nd.addNews(ntid,ntitle,nauthor,ncontent,nsummary);
    		}else{
    			row=NewsDao.nd.addNews(ntid,ntitle,nauthor,"images/"+newFileName,ncontent,nsummary);
    		}
    		if(row>0){
    			%>
				<h2>增加成功</h2>
				<input type="button" onclick="javascript:window.location='addnews.jsp'" value="确定"></input>
				<%
    		}
    		}catch(FileUploadBase.SizeLimitExceededException e){
    			e.printStackTrace();
    			%>
				<h2>文件太大上传失败</h2>
				<input type="button" onclick="javascript:window.location='addnews.jsp'" value="返回"></input>
				<%
    			return;
    		}catch(Exception e){		
    			e.printStackTrace();
    			%>
				<h2>出现错误</h2>
				<input type="button" onclick="javascript:window.location='addnews.jsp'" value="返回"></input>
				<%
    			return;
    		}
    	}
    	}else if(opr.equals("findNew")){
    		request.getRequestDispatcher("../modifynews.jsp").forward(request, response);
    		return;
    	}else if(opr.equals("updateNews")){
    		Long ntid=null;
    		Long nid=null;
    		String ntitle=null;
    		String nauthor=null;
    		String nsummary=null;
    		String ncontent=null;
    		String newFileName=null;
    		boolean isMultipart=ServletFileUpload.isMultipartContent(request);
    		String uploadPath=application.getRealPath("uploadImg");
    		String uploadTempPath=application.getRealPath("uploadTemp");
    		File tempFile=new File(uploadTempPath);
    		if(!tempFile.exists()){
    			tempFile.mkdirs();
    		}
    		if(isMultipart){
    			DiskFileItemFactory factory=new DiskFileItemFactory();
    			factory.setSizeThreshold(4096);
    			factory.setRepository(tempFile);
    			ServletFileUpload upload=new ServletFileUpload(factory);
    			upload.setHeaderEncoding("utf-8");
    			upload.setSizeMax(1024*500);
    			try{
    				List<FileItem> items=upload.parseRequest(request);
    				for(FileItem f:items){
    					if(f.isFormField()){
    						if(f.getFieldName().equals("ntid")){
    							ntid=Long.parseLong(f.getString());
    						}else if(f.getFieldName().equals("nid")){
								nid=Long.parseLong(f.getString());
							}else if(f.getFieldName().equals("ntitle")){
								ntitle=f.getString("utf-8");
							}else if(f.getFieldName().equals("nauthor")){
								nauthor=f.getString("utf-8");
							}else if(f.getFieldName().equals("nsummary")){
								nsummary=f.getString("utf-8");
							}else if(f.getFieldName().equals("ncontent")){
								ncontent=f.getString("utf-8");
							}
    					}else{
    						if(f.getName()==null||f.getName().equals("")){
    							continue;
    						}
    						List<String> typeList=Arrays.asList("gif","bmp","jpg","png","jpeg");
							String FileType=f.getName().substring(f.getName().lastIndexOf(".")+1);
							if(typeList.contains(FileType)){
								newFileName=UUID.randomUUID().toString()+"."+FileType;
								f.write(new File(uploadPath,newFileName));
							}else{
							%>
							<h2>文件类型错误</h2>
							<input type="button" onclick="javascript:window.location='modifynews.jsp'" value="确定"></input>
							<%
							}
    					}
    				}
    				int row=0;
    				if(newFileName!=null){
    					row=NewsDao.nd.updateNews(ntid,ntitle,nauthor,"images/"+newFileName,ncontent,nsummary,nid);
    				}else{
    					row=NewsDao.nd.updateNews(ntid,ntitle,nauthor,ncontent,nsummary,nid);
    				}
    				if(row>0){
    				%>
				<h2>增加成功</h2>
				<input type="button" onclick="javascript:window.location='admin.jsp'" value="确定"></input>
				<%
    				}
    			}catch(FileUploadBase.FileSizeLimitExceededException e){
    			%>
				<h2>文件太大上传失败</h2>
				<input type="button" onclick="javascript:window.location='admin.jsp'" value="返回"></input>
				<%
    			return;
    			}catch(Exception e){		
    			e.printStackTrace();
    			%>
				<h2>出现错误</h2>
				<input type="button" onclick="javascript:window.location='admin.jsp'" value="返回"></input>
				<%
    			return;
    		}
    		}
    	}
    		
     %>

    </ul>
  </div>
</div>
<div id="footer">
  <div id="site_link"> <a href="#">关于我们</a><span>|</span> <a href="#">Aboue Us</a><span>|</span> <a href="#">联系我们</a><span>|</span> <a href="#">广告服务</a><span>|</span> <a href="#">供稿服务</a><span>|</span> <a href="#">法律声明</a><span>|</span> <a href="#">招聘信息</a><span>|</span> <a href="#">网站地图</a><span>|</span> <a href="#">留言反馈</a> </div>
<div id="footer">
  <p class="">24小时客户服务热线：010-68988888  &#160;&#160;&#160;&#160; <a href="#">常见问题解答</a> &#160;&#160;&#160;&#160;  新闻热线：010-627488888<br />
    文明办网文明上网举报电话：010-627488888  &#160;&#160;&#160;&#160;  举报邮箱：<a href="#">jubao@jb-aptech.com.cn</a></p>
  <p class="copyright">Copyright &copy; 1999-2009 News China gov, All Right Reserver<br />
    新闻中国   版权所有</p>
</div>
</body>
</html>
